<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>向套接字发送消息，无论它是否已建立连接</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.socket-sendmsg.html">« socket_sendmsg</a></li>
      <li style="float: right;"><a href="function.socket-set-block.html">socket_set_block »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.sockets.html">Socket 函数</a></li>
    <li>向套接字发送消息，无论它是否已建立连接</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.socket-sendto" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">socket_sendto</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.1.0, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">socket_sendto</span> &mdash; <span class="dc-title">向套接字发送消息，无论它是否已建立连接</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.socket-sendto-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>socket_sendto</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="class.socket.html" class="type Socket">Socket</a></span> <code class="parameter">$socket</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$data</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$flags</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$address</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">?</span><span class="type"><span class="type">int</span><span class="type"></span></span> <code class="parameter">$port</code><span class="initializer"> = <strong><code>null</code></strong></span></span><br>): <span class="type"><span class="type">int</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   函数 <span class="function"><strong>socket_sendto()</strong></span> 通过套接字 <code class="parameter">socket</code> 
   从参数 <code class="parameter">data</code> 中发送 <code class="parameter">length</code> 
   指定的字节数到 <code class="parameter">address</code> 地址的 <code class="parameter">port</code> 端口。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.socket-sendto-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">socket</code></dt>

     <dd>

      <p class="para">
       由 <span class="function"><a href="function.socket-create.html" class="function">socket_create()</a></span> 创建的 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span> 实例。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">data</code></dt>

     <dd>

      <p class="para">
       要从 <code class="parameter">data</code> 缓冲区发送的数据。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">data</code> 中要发送的 <code class="parameter">length</code> 长度的字节数。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">flags</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">flags</code> 的值可以是下列任意 flag 的组合。使用按位或运算符（<code class="literal">|</code>）来组合不同的 flag。
       <table class="doctable table">
        <caption><strong><code class="parameter">flags</code> 可用值</strong></caption>
        
         <tbody class="tbody">
          <tr>
           <td><strong><code>MSG_OOB</code></strong></td>
           <td>
            发送 OOB（带外）数据。
           </td>
          </tr>

          <tr>
           <td><strong><code>MSG_EOR</code></strong></td>
           <td>
            逻辑记录标记。发送的数据结束一个逻辑记录。
           </td>
          </tr>

          <tr>
           <td><strong><code>MSG_EOF</code></strong></td>
           <td>
            从发送方关闭套接字，并在指定数据末尾设置结束标识。发送数据后结束事务。
           </td>
          </tr>

          <tr>
           <td><strong><code>MSG_DONTROUTE</code></strong></td>
           <td>
            绕过路由，直接使用接口。
           </td>
          </tr>

         </tbody>
        
       </table>

      </p>
     </dd>

    
    
     <dt>
<code class="parameter">address</code></dt>

     <dd>

      <p class="para">
       远端 IP 地址。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">port</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">port</code> 是数据要被发送到的远端端口号。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.socket-sendto-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   <span class="function"><strong>socket_sendto()</strong></span> 返回发送到远端地址的字节数。发生错误时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.socket-sendto-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
  <td>8.0.0</td>
  <td>
   现在 <code class="parameter">socket</code> 是 <span class="classname"><a href="class.socket.html" class="classname">Socket</a></span> 实例，
   之前是 <span class="type">resource</span>。
  </td>
 </tr>

     <tr>
      <td>8.0.0</td>
      <td>
       <code class="parameter">port</code> 现在允许为 null。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.socket-sendto-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4505">
    <p><strong>示例 #1 <span class="function"><strong>socket_sendto()</strong></span> 示例</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />&nbsp;&nbsp;&nbsp;&nbsp;$sock&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">socket_create</span><span style="color: #007700">(</span><span style="color: #0000BB">AF_INET</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOCK_DGRAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SOL_UDP</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$msg&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">"Ping&nbsp;!"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$len&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$msg</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">socket_sendto</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$msg</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$len</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'127.0.0.1'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">1223</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">socket_close</span><span style="color: #007700">(</span><span style="color: #0000BB">$sock</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.socket-sendto-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.socket-send.html" class="function" rel="rdfs-seeAlso">socket_send()</a> - 向已连接的套接字发送数据</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>